package com.ruipengkj.component.jdialog;
	
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JPanel;

import com.ruipengkj.view.MoveListener;
import com.ruipengkj.view.base.BaseDialog;
import com.sun.awt.AWTUtilities;
	
public class IDialog extends BaseDialog implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	private TitlePanel titleBar;
	private Component iContentPane;
	
	private String title;
	public IDialog(JDialog jd, String title, boolean b) {
		super(jd, "", b);
		this.title = title;
		setUndecorated(true);
		initialize();
//		this.setSize(new Dimension(300, 400));
		AWTUtilities.setWindowOpaque(this, false);
		
		MoveListener moveListener = new MoveListener();
		this.addMouseListener(moveListener);
		this.addMouseMotionListener(moveListener);
	}
	
	private void initialize()
	{
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(getTitleBar(), BorderLayout.NORTH);
		getContentPane().add(getIContentPane(), BorderLayout.CENTER);
	}
	
	@Override
	public void printComponents(Graphics g) {
		super.printComponents(g);
		g.setColor(new Color(241, 133, 21));
		g.drawLine(0, 0, 100, this.getHeight());
		g.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1);
		g.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1);
	}
	
	public JPanel getTitleBar()
	{
		if (titleBar == null)
		{
			titleBar = new TitlePanel();
			Dimension dimension = getTitleSize();
			if (dimension == null)
				titleBar.setTitle(title, 93, 29);
			else
				titleBar.setTitle(title, dimension.width, dimension.height);
			titleBar.getCloseButton().addActionListener(this);
		}
		return titleBar;
	}
	
	protected Dimension getTitleSize() {
		return null;
	}
	
	public void setTitleBar(TitlePanel windowTitleBar)
	{
		getContentPane().remove(titleBar);
		titleBar = windowTitleBar;
		getContentPane().add(windowTitleBar, BorderLayout.NORTH);
		windowTitleBar.getCloseButton().addActionListener(this);
		repaint();
	}
	
	public Component getIContentPane()
	{
		if (iContentPane == null)
		{
			iContentPane = new JPanel();
		}
		return iContentPane;
	}	
	
	public void setIContentPane(Component contentPane)
	{
		getContentPane().add(contentPane, BorderLayout.CENTER);
		iContentPane = contentPane;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == titleBar.getCloseButton()) {
			this.dispose();
		}
	}
	public static void main(String[] args) {
		IDialog dialog = new IDialog(null, "添加菜品", true);
		dialog.setVisible(true);
	}
}
